﻿//by  张海军 710605420@qq.com
using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 看框架喜好，可以选择JobComponentSystem，或者ComponentSystem，也可以不用这两个，直接把update  接管到UGUIManager里面让整个UI系统的update统一。具体可以看git早期log。实际上UI应该用不到 fixupdate,lateupdate,这里只是做一个框架上的完整性演示。
/// </summary>
namespace LazyFramework.UI
{
    [BurstCompile]
    [AlwaysUpdateSystem]
    [RequireComponent(typeof(Canvas))]
    [RequireComponent(typeof(CanvasScaler))]
    [RequireComponent(typeof(GraphicRaycaster))]
    public class UIViewBase : ComponentSystem, UIViewInterface
    {
        protected UIType uiType = UIType.eBegin;
        protected UILayer uiLayer = UILayer.eMainLayer;
        public bool is3D = false;

        public float uiTTF = 0;
        public GameObject Actor = null;

        protected Canvas uiCanvas = null;
        protected CanvasScaler uiCanvasScaler = null;
        protected GraphicRaycaster uiGraphicRaycaster = null;


        protected virtual void OnInitialize() { }
        protected virtual void OnOpen() { }
        protected virtual void OnClose() { }
        protected virtual void OnRelease() { }
        protected virtual void OnLayerChanged(int layer) { }
        protected virtual void OnSortOrderChanged(int sortOrder) { }

       // private List<Transform> uiTrans = new List<Transform>();

        public Canvas GetCanvas()
        {
            return uiCanvas;
        }
        public CanvasScaler GetCanvasScaler()
        {
            return uiCanvasScaler;
        }
        public GraphicRaycaster GetGraphicRaycaster()
        {
            return uiGraphicRaycaster;
        }

        public virtual UIType GetUIType()
        {
            return uiType;
        }

        public virtual UILayer GetUILayer()
        {
            return uiLayer;
        }
       

        public void Initialize()
        {
            if (uiCanvas == null)
            {
                uiCanvas = Actor.GetComponent<Canvas>();
            }
            if (uiCanvasScaler == null)
            {
                uiCanvasScaler = Actor.GetComponent<CanvasScaler>();
            }
            if (uiGraphicRaycaster == null)
            {
                uiGraphicRaycaster = Actor.GetComponent<GraphicRaycaster>();
            }
            OnInitialize();

        }
        public void Open()
        {
            uiCanvas.enabled = true;
            uiCanvasScaler.enabled = true;
            uiGraphicRaycaster.enabled = true;
            uiCanvas.additionalShaderChannels = AdditionalCanvasShaderChannels.Normal
                |AdditionalCanvasShaderChannels.TexCoord1 
                | AdditionalCanvasShaderChannels.TexCoord2 
                | AdditionalCanvasShaderChannels.TexCoord3;

            OnOpen();
        }
        public void Close()
        {
            OnClose();
        }
        public void Release()
        {
            OnRelease();
           
        }


        public virtual void FixedUpdate()
        {

        }

        public virtual void LateUpdate()
        {

        }

        public void SetLayer(int layerMask)
        {
            LazyFrameworkUtils.SetLayer(Actor, layerMask);
            OnLayerChanged(layerMask);
        }

        public void SetSortOrder(int order)
        {
           if(uiCanvas != null)
            {
                uiCanvas.sortingOrder = order;
            }
            OnSortOrderChanged(order);
        }

     
        protected override void OnUpdate()
        {
           
        }
    }

}
